
#include "MainWindow.h"

MainWindow::MainWindow()
{
	_motion = new Motion();

	BScene* scene = new BScene();

	BSurface* surface = new BSurface(20, 20, 10, 10);
	surface->setOrientation(Orientation_Vertical);
	surface->setPolyMode(Poly_Line);
	surface->setColor(255, 130, 60);
	surface->setLighting(false);
	surface->setPosition(-10, -10, 0);
	scene->insert(surface);

	_sphere = new BSphere(1, 12, 12);
	_sphere->setColor(255, 0, 0);
	scene->insert(_sphere);

	BCamera* camera = new BCamera();
	camera->setPosition(0, -16, 0);
	camera->setCenter(0, 0, 0);
	camera->setFovy(36);
	camera->setNear(BReal(0.1));
	camera->setFar(1000);
	camera->setColor(23, 34, 45);

	_viewer = new BViewer();
	_viewer->setScene(scene);
	_viewer->setCamera(camera);
	_viewer->setHandler(new BRoundHandler());

	BIcon* icon = ReadFile<BIcon>("./data/image/neon_013.png");
	_resetButton = new BButton(icon, "reset");
	_playButton = new BButton("play");
	_pauseButton = new BButton("pause");
	_comboBox = new BComboBox();
	_comboBox->addItem("None", Motion_None);
	_comboBox->addItem("Linear", Motion_Linear);
	_comboBox->addItem("OutBounce", Motion_OutBounce);
	_comboBox->addItem("InBounce", Motion_InBounce);
	_comboBox->addItem("InOutBounce", Motion_InOutBounce);
	_comboBox->addItem("OutQuad", Motion_OutQuad);
	_comboBox->addItem("InQuad", Motion_InQuad);
	_comboBox->addItem("InOutQuad", Motion_InOutQuad);
	_comboBox->addItem("OutCubic", Motion_OutCubic);
	_comboBox->addItem("InCubic", Motion_InCubic);
	_comboBox->addItem("InOutCubic", Motion_InOutCubic);
	_comboBox->addItem("InQuart", Motion_InQuart);
	_comboBox->addItem("OutQuart", Motion_OutQuart);
	_comboBox->addItem("InOutQuart", Motion_InOutQuart);
	_comboBox->addItem("OutElastic", Motion_OutElastic);
	_comboBox->addItem("InElastic", Motion_InElastic);
	_comboBox->addItem("InOutElastic", Motion_InOutElastic);
	_comboBox->addItem("OutSine", Motion_OutSine);
	_comboBox->addItem("InSine", Motion_InSine);
	_comboBox->addItem("InOutSine", Motion_InOutSine);
	_comboBox->addItem("OutBack", Motion_OutBack);
	_comboBox->addItem("InBack", Motion_InBack);
	_comboBox->addItem("InOutBack", Motion_InOutBack);
	_comboBox->addItem("OutCirc", Motion_OutCirc);
	_comboBox->addItem("InCirc", Motion_InCirc);
	_comboBox->addItem("InOutCirc", Motion_InOutCirc);
	_comboBox->addItem("OutExpo", Motion_OutExpo);
	_comboBox->addItem("InExpo", Motion_InExpo);
	_comboBox->addItem("InOutExpo", Motion_InOutExpo);
	_comboBox->setCurrentIndex(0);
	_comboBox->setWidthPolicy(Policy_Preferred);

	_loopCheckBox = new BCheckBox("loop");

	BHBoxLayout* hlayout = new BHBoxLayout();
	hlayout->addWidget(_resetButton);
	hlayout->addWidget(_playButton);
	hlayout->addWidget(_pauseButton);
	hlayout->addWidget(_comboBox);
	hlayout->addWidget(_loopCheckBox);
	hlayout->addSpring();
	hlayout->setWidthPolicy(Policy_Preferred);

	BVBoxLayout* layout = new BVBoxLayout(this);
	layout->addWidget(_viewer, 100);
	layout->addLayout(hlayout);
	layout->setAlign(1, Align_Center);

	connect(_resetButton, Signal_Clicked, &MainWindow::slotResetClicked);
	connect(_playButton, Signal_Clicked, &MainWindow::slotPlayClicked);
	connect(_pauseButton, Signal_Clicked, &MainWindow::slotPauseClicked);
	connect(_comboBox, Signal_CurrentChanged, &MainWindow::slotCurrentChanged);
	connect(_loopCheckBox, Signal_Checked, &MainWindow::slotLoopChecked);
	_loopCheckBox->setChecked(true);
}

void MainWindow::slotPlayClicked(BObject* object, const BValue& value)
{
	_motion->start();
}
void MainWindow::slotResetClicked(BObject* object, const BValue& value)
{
	_motion->reset();
}
void MainWindow::slotPauseClicked(BObject* object, const BValue& value)
{
	_motion->pause();
}
void MainWindow::slotCurrentChanged(BObject* object, const BValue& value)
{
	BComboItem* item = value;
	MotionType type = item->value();
	_motion->setType(type);
}
void MainWindow::slotLoopChecked(BObject* object, const BValue& value)
{
	_motion->setLoop(value);
}

void MainWindow::showEvent(const BEvent& event)
{
	BMainWindow::showEvent(event);
	BRoundHandler* handler = (BRoundHandler*)_viewer->handler();
	handler->focus();
}

void MainWindow::updateEvent(const BEvent& event)
{
	if (_motion && _motion->started())
	{
		double time = event.time();
		_motion->update(time);
		float x = (float)_motion->value() * 10;
		BVector pos = _sphere->position();
		pos.x() = x;
		_sphere->setPosition(pos);
	}
}
